import HttpUtils from '../HttpUtils/HttpUtils';// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
Page({
	...HttpUtils.func,
	data: {
		folderCode: 0,
		pageIndex: 1,
		pageItems: 10,
		totalPages: 0,
		topicList: []
	},
	topicDetail(e) {
		var folderCode = e.currentTarget.dataset.foldercode;	
		var topicName = e.currentTarget.dataset.topicName;
		var code = e.currentTarget.dataset.code;
		console.log(folderCode + "====" + code);
		wx.navigateTo({
			url: '../topicDetail/topicDetail?code=' + code + "&folderCode=" + folderCode + "&topicName=" + topicName
		})
	},
	onLoad(params) {
		var folderCode = params.folderCode;
		this.setData({
			folderCode: folderCode
		});
		this.loadContentList();

	},
	loadContentList: function(e) {
		//加载栏目下内容
		var folderCode = this.data.folderCode;
		var newUrl = getApp().globalData.dangjianUrl + "/content/relativeContentQuery";
		var params = {};
		params["pageIndex"] = this.data.pageIndex + "";
		params["pageItems"] = this.data.pageItems + "";
		params["code"] = folderCode + "";
		params["siteCode"] = getApp().globalData.siteCode + "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "专题列表页下内容加载失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var contents = respData.data.contents;
					var totalPages = contents.totalPages;
					contents = contents.content;
					var contentList = [];
					for (var index in contents) {
						var content = contents[index];
						var json = {};
						var thumbnail = content.thumbnail;
						if (null == thumbnail || "" == thumbnail) {
							var imageFiles = content.imageFiles;
							for (var j in imageFiles) {
								thumbnail = imageFiles[j].url;
								break;
							}
						}
						json["name"] = content.name;
						json["publishTime"] = content.publishTime;
						json["url"] = thumbnail;
						json["code"] = content.code;
						json["type"] = content.type;
						json["foldCode"] = folderCode;
						json["count"] = content.count;
						json["fromNa"] = '' == content.fromNa ? "华数" : content.fromNa;
						json["folderCode"] = folderCode;
						contentList.push(json);
					}
					this.setData({
						"topicList": contentList,
						"totalPages": totalPages
					});
				} else {
          this.showPageMsg("alert", respData.data.result.description)  
				}
			}
		);
	},
	onReachBottom: function(e) {
		var pageIndex = this.data.pageIndex + 1;
		var totalPages = this.data.totalPages;
		if (totalPages >= pageIndex) {
			this.setData({
				"pageIndex": pageIndex
			});
			this.loadContentList();
		} else {
			this.showPageMsg("tips", "没有更多数据了");
		}
	},
	onPullDownRefresh: function() {
		this.setData({
			pageIndex: 1,
			topicList: []
		});
		this.loadContentList();
		wx.stopPullDownRefresh();
	}
});
